﻿using Caliburn.Micro;
using KouDai.Phone.Engine;
using KouDai.Phone.Helper;
using KouDai.Phone.Models;
using KouDai.Phone.Services;
using KouDai.Phone.Utils;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace KouDai.Phone.ViewModels
{
    public class ImageWallViewModel : KouDaiScreen
    {
        #region Fields
        private ObservableCollection<HoneyEntity> _items = new ObservableCollection<HoneyEntity>();
        private INavigationService _navigationService;
        private long _minTimeStamp = 0;//上拉获取老数据的时间戳
        private bool _isLoading;
        #endregion

        #region Constructor
        public ImageWallViewModel()
        {
            if (Execute.InDesignMode)
            {
                LoadDesignData();
            }
        }

        public ImageWallViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
        }
        #endregion

        #region Properties
        public ObservableCollection<HoneyEntity> Items
        {
            get { return _items; }
            set
            {
                if (value != _items)
                {
                    _items = value;
                    this.NotifyOfPropertyChange(() => this.Items);
                }
            }
        }
        public string CombineAppId { get; set; }

        public bool IsLoading
        {
            get { return _isLoading; }
            set { _isLoading = value; }
        }
        #endregion


        #region override

        public override void LoadDesignData()
        {
            List<HoneyEntity> honeyList = new List<HoneyEntity>();
            for (int i = 0; i < 10; i++)
            {
                honeyList.Add(new HoneyEntity { ImageUrlForIphone = "http://img.geilicdn.com/taobao18553835260_146x146.jpg", Name = "2013新款韩版甜美淑女毛毛包夹子包毛绒绒手拿斜挎单肩女包包" });
            }
            honeyList.ToObservableCollection(Items);
        }

        protected override void OnInitialize()
        {
            GetImageWall();
        }
        #endregion

        #region ViewMethod
        public async void GetImageWall()
        {
            if (!IsLoading)
            {
                BackData backData = await ServiceManager.GetImageWall(this.CombineAppId, 0);
                if (backData.IsSuccess)
                {
                    List<HoneyEntity> honeyList = backData.ResposeData as List<HoneyEntity>;
                    honeyList.ToObservableCollection<HoneyEntity>(this.Items);
                    HoneyEntity lastEntity = honeyList[honeyList.Count - 1];
                    this._minTimeStamp = lastEntity.Timestamp;
                }
                else
                {
                    Debug.WriteLine(backData.Msg.ToString());
                }
                this.IsLoading = false;
            }
        }

        public void PullDownToFresh()
        {
            _minTimeStamp = 0;
            Items.Clear();
            GetImageWall();
        }

        public void ImageTap(HoneyEntity selectedItem)
        {
            Debug.WriteLine("url:" + selectedItem.ImageUrlForIphone);
        }
        #endregion

        #region Method
        
        
        #endregion

    }
}
